﻿{extend name="public:base" /}

{block name="css"}
<link href="__ADMIN__/css/tree.css" rel="stylesheet">
<style>
    .city-box {
        display: flex;
        flex-flow: row wrap;
    }

    .all_fold, .fold {
        padding-top: 10px;
    }

    .weight_region, .count_region {
        font-size: 14px;
    }

    .top-module{
        display: flex;
    }
</style>
{/block}

{block name="content"}

<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox ">
                <div class="ibox-content">
                    <form method="post">
                        <div class="box-item">
                            <div class="form-group row">
                                <label class="col-sm-1 col-form-label">模板名称</label>
                                <div class="col-sm-3">
                                    <input type="text" class="form-control" name="name" placeholder="请输入模板名称" value="{$info.name}">
                                </div>
                            </div>
                            <div class="hr-line-dashed"></div>
                        </div>

                        <div class="box-item">
                            <div class="form-group row">
                                <label class="col-sm-1 col-form-label">计费方式</label>
                                <div class="col-sm-3">
                                    <div class="i-checks">
                                        <label><input type="radio" value="1" name="type" {$info.type !=2 ? 'checked' :
                                            ''}> 件数 </label>
                                    </div>
                                    <div class="i-checks">
                                        <label><input type="radio" value="2" name="type" {$info.type== 2 ? 'checked' :
                                            ''}> 重量 </label>
                                    </div>
                                </div>
                            </div>
                            <div class="hr-line-dashed"></div>
                        </div>

                        <!--    按件    -->
                        <div class="box-item freight_count">
                            <div class="form-group row">
                                <label class="col-sm-1 col-form-label">计价规则</label>
                                <label class="col-form-label">
                                    默认运费
                                </label>
                                <div class="col-sm-1">
                                    <input type="number" class="form-control" name="init_count" min="1" value="{$info.init_count}">
                                </div>
                                <label class="col-form-label">
                                    件内
                                </label>
                                <div class="col-sm-1">
                                    <input type="number" class="form-control" name="init_count_price" min="0" value="{$info.init_count_price}">
                                </div>
                                <label class="col-form-label">
                                    元，每增加
                                </label>
                                <div class="col-sm-1">
                                    <input type="number" class="form-control" name="inc_count" min="1" value="{$info.inc_count}">
                                </div>
                                <label class="col-form-label">
                                    件，增加运费
                                </label>
                                <div class="col-sm-1">
                                    <input type="number" class="form-control" name="inc_count_price" min="0" value="{$info.inc_count_price}">
                                </div>
                                <label class="col-form-label">
                                    元
                                </label>
                            </div>
                            <div class="hr-line-dashed"></div>
                        </div>
                        <!--    按重量    -->
                        <div class="box-item freight_weight" style="display: none">
                            <div class="form-group row"><label class="col-sm-1 col-form-label">计价规则</label>
                                <label class="col-form-label">
                                    默认运费
                                </label>
                                <div class="col-sm-1">
                                    <input type="number" class="form-control" name="init_weight" min="0" value="{$info.init_weight}">
                                </div>
                                <label class="col-form-label">
                                    KG内
                                </label>
                                <div class="col-sm-1">
                                    <input type="number" class="form-control" name="init_weight_price" min="0" value="{$info.init_weight_price}">
                                </div>
                                <label class="col-form-label">
                                    元，每增加
                                </label>
                                <div class="col-sm-1">
                                    <input type="number" class="form-control" name="inc_weight" min="0" value="{$info.inc_weight}">
                                </div>
                                <label class="col-form-label">
                                    KG，增加运费
                                </label>
                                <div class="col-sm-1">
                                    <input type="number" class="form-control" name="inc_weight_price" min="0" value="{$info.inc_weight_price}">
                                </div>
                                <label class="col-form-label">
                                    元
                                </label>
                            </div>
                            <div class="hr-line-dashed"></div>
                        </div>


                        <button type="button" class="btn btn-w-m btn-primary" data-toggle="modal" onclick="addModelOpen()">
                            新增城市运费计价规则
                        </button>
                        <!--    按件    -->
                        <table class="table table-striped count_table">
                            <thead>
                            <tr>
                                <th width="40%">配送区域</th>
                                <th width="10%">首件（件）</th>
                                <th width="10%">运费（元）</th>
                                <th width="10%">续件（件）</th>
                                <th width="10%">续费（元）</th>
                                <th width="20%">操作</th>
                            </tr>
                            </thead>
                            <tbody class="count_tbody">
                            {if ($info.type == 1)}
                            {volist name="info.diff_freight_list" id="vo"}
                            <tr>
                                <td class="count_region">{$vo.province_text}{if ($vo.province_text && $vo.city_text)},{/if}{$vo.city_text}</td>
                                <td>
                                    <input type="number" class="form-control diff_init_count" min="1" onblur="changeDiffInitCount(this)" value="{$vo.diff_init_count}">
                                </td>
                                <td>
                                    <input type="number" class="form-control diff_init_count_price" min="0" onblur="changeDiffInitCountPrice(this)" value="{$vo.diff_init_count_price}">
                                </td>
                                <td>
                                    <input type="number" class="form-control diff_inc_count" min="1" onblur="changeDiffIncCount(this)" value="{$vo.diff_inc_count}">
                                </td>
                                <td>
                                    <input type="number" class="form-control diff_inc_count_price" min="0" onblur="changeDiffIncCountPrice(this)" value="{$vo.diff_inc_count_price}">
                                </td>
                                <td class="project-actions">
                                    <a onclick="setRegion({$key})" class="btn btn-white btn-sm"><i class="iconfont icon-edit"></i>
                                        设置城市 </a>
                                    <a onclick="delRegion({$key})" class="btn btn-white btn-sm"><i class="iconfont icon-trash"></i>
                                        删除 </a>
                                </td>
                            </tr>
                            {/volist}
                            {/if}
                            </tbody>
                        </table>
                        <!--    按重量    -->
                        <table class="table table-striped weight_table" style="display: none">
                            <thead>
                            <tr>
                                <th width="40%">配送区域</th>
                                <th width="10%">首件（KG）</th>
                                <th width="10%">运费（元）</th>
                                <th width="10%">续件（KG）</th>
                                <th width="10%">续费（元）</th>
                                <th width="20%">操作</th>
                            </tr>
                            </thead>
                            <tbody class="weight_tbody">
                            {if ($info.type == 2)}
                            {volist name="info.diff_freight_list" id="vo"}
                            <tr>
                                <td class="weight_region">{$vo.province_text}{if ($vo.province_text &&
                                    $vo.city_text)},{/if}{$vo.city_text}</td>
                                <td>
                                    <input type="number" class="form-control diff_init_weight" min="0" onblur="changeDiffInitWeight(this)" value="{$vo.diff_init_weight}">
                                </td>
                                <td>
                                    <input type="number" class="form-control diff_init_weight_price" min="0" onblur="changeDiffInitWeightPrice(this)" value="{$vo.diff_init_weight_price}">
                                </td>
                                <td>
                                    <input type="number" class="form-control diff_inc_weight" min="0" onblur="changeDiffIncWeigth(this)" value="{$vo.diff_inc_weight}">
                                </td>
                                <td>
                                    <input type="number" class="form-control diff_inc_weight_price" min="0" onblur="changeDiffIncWeightPrice(this)" value="{$vo.diff_inc_weight_price}">
                                </td>
                                <td class="project-actions">
                                    <a onclick="setRegion({$key})" class="btn btn-white btn-sm"><i class="iconfont icon-edit"></i>
                                        设置城市 </a>
                                    <a onclick="delRegion({$key})" class="btn btn-white btn-sm"><i class="iconfont icon-trash"></i>
                                        删除 </a>
                                </td>
                            </tr>
                            {/volist}
                            {/if}
                            </tbody>
                        </table>

                        <div class="hr-line-dashed"></div>

                        <div style="display: none" class="diff_freight"></div>

                        <input type="hidden" name="id" value="{:input('id')}" style="display: none">
                        <input type="hidden" name="diff_freight" value='{$info.diff_freight}'>

                        <div class="form-group row">
                            <div class="col-sm-4 col-sm-offset-2">
                                <button type="button" class="btn btn-w-m btn-default back-page">返回</button>
                                <button type="button" class="btn btn-w-m btn-primary ajax-post">提交</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <!--    城市选择弹出框    -->
    <div class="modal inmodal" id="myModal3" tabindex="-1" role="dialog" aria-hidden="true">
        <div class="modal-dialog" style="margin: 2rem 18rem;">
            <div class="modal-content animated fadeIn" style="width: 270%">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" onclick="modelClose()">
                        <span aria-hidden="true">&times;</span>
                        <span class="sr-only">Close</span>
                    </button>
                    <h4 class="modal-title">设置城市</h4>
                </div>
                <div class="modal-body city-modal" style="height: 620px; overflow:hidden; overflow-y: auto;">
                    <div class="all">
                        <div class="top-menu box">
                            <div class="top-module">
                                <label>
                                    <div class="checkbox-input">
                                        <input name="ids[]" class="all_country" type="checkbox" value="{$v.id}">
                                    </div>
                                    <span class="checkbox-name">全国</span>
                                </label>
                                <a class="all_fold checkbox-name">
                                    <i class="iconfont icon-chevron-up"></i>
                                </a>
                            </div>
                        </div>
                    </div>

                    {volist name="province_list" id="v" key='k'}
                    <div class="one box box-top-10">
                        <div class="top-menu">
                            <div class="top-module">
                                <label>
                                    <div class="checkbox-input">
                                        <input name="province_ids[]" class="one_check" type="checkbox" value="{$v.province_id}" onclick="oneTreeAll(this, '{$k - 1}')">
                                    </div>
                                    <span class="checkbox-name province_name">{$v.province_name}</span>
                                </label>
                                <a class="fold checkbox-name">
                                    <i class="iconfont icon-chevron-down"></i>
                                </a>
                            </div>
                        </div>

                        <!-- 循环城市 -->
                        <div class="city-box" style="display: none">
                            {volist name="v.city_list" id='vo' key='ke'}
                            <div class="two">
                                <div class="next">
                                    <div class="group-menu">
                                        <div class="module">
                                            <div style="float: left">
                                                <label>
                                                    <div class="checkbox-input">
                                                        <input name="city_ids[]" class="two_check" type="checkbox" value="{$vo.city_id}" onclick="twoTreeAll(this, '{$k - 1}', '{$ke - 1}')">
                                                    </div>
                                                    <span class="checkbox-name city_name">{$vo.city_name}</span>
                                                </label>
                                            </div>
                                        </div>
                                    </div>
                                    <div style="clear: both"></div>
                                </div>
                            </div>
                            {/volist}
                        </div>
                    </div>
                    {/volist}
                </div>

                <div class="modal-footer">
                    <button type="button" class="btn btn-white" onclick="modelClose()">关闭</button>
                    <button type="button" class="btn btn-primary" id="confirmRegion">保存</button>
                </div>
            </div>
        </div>
    </div>
</div>

{/block}

{block name='js'}
<script type="text/javascript" src="__PLUG__/kindeditor/kindeditor-all.js"></script>
<script src="__ADMIN__/js/freight.js"></script>
<script>
    $(function (){
        var type = $('input:radio[name="type"]:checked').val();
        if (type == 1) {
            $('.freight_count').show();
            $('.freight_weight').hide();
            $('.count_table').show();
            $('.count_table tbody').show();
            $('.weight_table').hide();
            $('.weight_table tbody').hide();
        } else {
            $('.freight_weight').show();
            $('.freight_count').hide();
            $('.weight_table').show();
            $('.weight_table tbody').show();
            $('.count_table').hide();
            $('.count_table tbody').hide();
        }
    })

    $('input:radio[name="type"]').change(function (){
        diff_freight_data = [];

        if($(this).val() == 1){
            // 数量
            $('.freight_count').show();
            $('.freight_weight').hide();
            $('.freight_weight').find("input").val('');

            $('.count_table').show();
            $('.weight_table').hide();
        }else{
            // 重量
            $('.freight_weight').show();
            $('.freight_count').hide();
            $('.freight_count').find("input").val('');

            $('.weight_table').show();
            $('.count_table').hide();
        }

        renderDiffTr();
        deleteForbiddenChecked(diff_freight_data)
    })

    var model_type;//1：新增城市运费计价规则，2：设置城市
    var set_city_index;//设置城市的下标

    // 打开 城市选择 弹框；新增城市运费计价规则
    function addModelOpen() {
        model_type = 1;
        $('body').addClass('modal-open').append('<div class="modal-backdrop show"></div>');
        $('#myModal3').css('display', 'block').addClass('show');
        forbiddenChecked();
    }

    // 打开 城市选择 弹框 ；设置城市
    function setRegion(index) {
        model_type = 2;
        set_city_index = index;
        $('body').addClass('modal-open').append('<div class="modal-backdrop show"></div>');
        $('#myModal3').css('display', 'block').addClass('show');
        forbiddenChecked();
    }

    // 删除 配送区域
    function delRegion(index){
        var delete_diff_freight_data = [diff_freight_data[index]];
        diff_freight_data.splice(index, 1);

        $("input[name='diff_freight']").val(JSON.stringify(diff_freight_data));

        deleteForbiddenChecked(delete_diff_freight_data);
        renderDiffTr();
    }

    // 打开弹框禁用已选中的复选框
    function forbiddenChecked() {
        if (diff_freight_data.length > 0) {
            diff_freight_data.forEach(function (val, index) {
                if (val.is_all) {
                    $('.city-modal').find("input").prop("checked", true).prop("disabled", true);
                } else {
                    // 禁用省
                    if (val.province_ids) {
                        for (var i = 0; i < $('.one_check').length; i++) {
                            var province_id = $('.one_check').eq(i).val();
                            if (val.province_ids.indexOf(province_id) != -1) {
                                $('.one').eq(i).find("input").prop("checked", true).prop("disabled", true);
                            }
                        }
                    }

                    // 禁用市
                    if (val.city_ids) {
                        var city_ids_arr = val.city_ids.split(',');
                        city_ids_arr.forEach(function (v, k) {
                            $("input[name='city_ids[]'][value='" + v + "']").prop("checked", true).prop("disabled", true);
                        })
                    }
                }
            })

            // 设置城市启用部分复选框
            if(model_type == 2){
                var open_data = diff_freight_data[set_city_index];
                if (open_data.is_all) {
                    $('.city-modal').find("input").prop("disabled", false);
                } else {
                    // 禁用省
                    if (open_data.province_ids) {
                        for (var i = 0; i < $('.one_check').length; i++) {
                            var province_id = $('.one_check').eq(i).val();
                            if (open_data.province_ids.indexOf(province_id) != -1) {
                                $('.one').eq(i).find("input").prop("disabled", false);
                            }
                        }
                    }

                    // 禁用市
                    if (open_data.city_ids) {
                        var city_ids_arr = open_data.city_ids.split(',');
                        city_ids_arr.forEach(function (v, k) {
                            $("input[name='city_ids[]'][value='" + v + "']").prop("disabled", false);
                        })
                    }
                }
            }
        }
    }

    // 打开弹框启用已选中的复选框
    function deleteForbiddenChecked(delete_diff_freight_data) {
        if (delete_diff_freight_data.length > 0) {
            delete_diff_freight_data.forEach(function (val, index) {
                if (val.is_all) {
                    $('.city-modal').find("input").prop("checked", false).prop("disabled", false);
                } else {
                    // 禁用省
                    if (val.province_ids) {
                        for (var i = 0; i < $('.one_check').length; i++) {
                            var province_id = $('.one_check').eq(i).val();
                            if (val.province_ids.indexOf(province_id) != -1) {
                                $('.one').eq(i).find("input").prop("checked", false).prop("disabled", false);
                            }
                        }
                    }

                    // 禁用市
                    if (val.city_ids) {
                        var city_ids_arr = val.city_ids.split(',');
                        city_ids_arr.forEach(function (v, k) {
                            $("input[name='city_ids[]'][value='" + v + "']").prop("checked", false).prop("disabled", false);;
                        })
                    }
                }
            })
        }else{
            $('.city-modal').find("input").prop("checked", false).prop("disabled", false);
        }
    }

    // 关闭 城市选择 弹框
    function modelClose() {
        $('body').removeClass('modal-open');
        $('.modal-backdrop').remove();
        $('#myModal3').css('display', 'none').removeClass('show');
    }

    // 全国折叠
    $('.all_fold').on('click', function (e) {
        e.preventDefault();
        var modal_body = $(this).closest('div.modal-body');
        var button = $(this).find('i');
        var content = modal_body.children('.one');
        content.slideToggle(200);
        button.toggleClass('icon-chevron-up').toggleClass('icon-chevron-down');
        modal_body.toggleClass('').toggleClass('border-bottom');
        setTimeout(function () {
            modal_body.resize();
            modal_body.find('[id^=map-]').resize();
        }, 50);
    });

    // 省份折叠
    $('.fold').on('click', function (e) {
        e.preventDefault();
        var one = $(this).closest('div.one');
        var button = $(this).find('i');
        var content = one.children('.city-box');
        content.slideToggle(200);
        button.toggleClass('icon-chevron-up').toggleClass('icon-chevron-down');
        one.toggleClass('').toggleClass('border-bottom');
        setTimeout(function () {
            one.resize();
            one.find('[id^=map-]').resize();
        }, 50);
    });

    var diff_freight_data = {$diff_freight_data};
    // 保存城市设置
    $('#confirmRegion').click(function () {
        //是否选中全国
        var all_country = $('.all_country').is(':checked');
        if (all_country && diff_freight_data.length == 0) {
            $('.diff_freight').html(1);

            var is_all = true;
            var province_ids = '';
            var province_text = '';
            var city_ids = '';
            var city_text = '';
        } else {
            var is_all = false;
            var province_ids = '';
            var province_text = '';
            var city_ids = '';
            var city_text = '';

            var all_one_checkbox = $('.one_check').length;
            //循环省份
            for (var i = 0; i < all_one_checkbox; i++) {
                //是否选中省份
                var province_check = $('.one_check').eq(i).is(':checked');
                if (province_check) {
                    // 查询该省份下面是否有城市被禁用（如果被禁用，说明在别的规则中已经使用该城市，一个城市不可被多个规则使用）
                    var disabled_len = $('.one').eq(i).find('.two_check:disabled').length;
                    if (disabled_len == 0) {
                        // 添加省数据
                        var province_id = $('.one_check').eq(i).val();
                        var province_name = $('.province_name').eq(i).text();
                        if (province_ids) {
                            province_ids += ',' + province_id;
                            province_text += ',' + province_name;
                        } else {
                            province_ids = province_id;
                            province_text = province_name;
                        }
                    } else {
                        // 添加城市数据
                        var all_two_checkbox = $('.one').eq(i).find('.two_check').length;
                        for (var j = 0; j < all_two_checkbox; j++) {
                            var city_check = $('.one').eq(i).find('.two_check').eq(j).is(':checked');
                            var city_disabled = $('.one').eq(i).find('.two_check').eq(j).is(':disabled');
                            if (city_check && !city_disabled) {
                                var city_id = $('.one').eq(i).find('.two_check').eq(j).val();
                                var city_name = $('.one').eq(i).find('.city_name').eq(j).text();
                                if (city_ids) {
                                    city_ids += ',' + city_id;
                                    city_text += ',' + city_name;
                                } else {
                                    city_ids = city_id;
                                    city_text = city_name;
                                }
                            }
                        }
                    }
                } else {
                    //是否选中城市
                    var all_two_checkbox = $('.one').eq(i).find('.two_check').length;
                    var all_two_have_check_length = $('.one').eq(i).find('input[type="checkbox"]:checked').length;
                    //循环城市
                    if (all_two_have_check_length) {
                        for (var j = 0; j < all_two_checkbox; j++) {
                            var city_check = $('.one').eq(i).find('.two_check').eq(j).is(':checked');
                            var city_disabled = $('.one').eq(i).find('.two_check').eq(j).is(':disabled');
                            if (city_check && !city_disabled) {
                                var city_id = $('.one').eq(i).find('.two_check').eq(j).val();
                                var city_name = $('.one').eq(i).find('.city_name').eq(j).text();
                                if (city_ids) {
                                    city_ids += ',' + city_id;
                                    city_text += ',' + city_name;
                                } else {
                                    city_ids = city_id;
                                    city_text = city_name;
                                }
                            }
                        }
                    }
                }
            }
        }

        if(province_text || city_text || is_all){
            var single_diff_freight = {
                is_all: is_all,
                province_ids: province_ids,
                province_text: province_text,
                city_ids: city_ids,
                city_text: city_text,
            }

            var type = $("input[name='type']:checked").val();
            if(type == 1){
                // 件数
                single_diff_freight.diff_init_count = '';
                single_diff_freight.diff_init_count_price = '';
                single_diff_freight.diff_inc_count = '';
                single_diff_freight.diff_inc_count_price = '';
            }else{
                // 重量
                single_diff_freight.diff_init_weight = '';
                single_diff_freight.diff_init_weight_price = '';
                single_diff_freight.diff_inc_weight = '';
                single_diff_freight.diff_inc_weight_price = '';
            }


            if (model_type == 1) {
                diff_freight_data.push(single_diff_freight);
            } else {
                diff_freight_data[set_city_index] = single_diff_freight;
            }

            $("input[name='diff_freight']").val(JSON.stringify(diff_freight_data))

            modelClose();
            renderDiffTr();
        }else{
            layer.msg('请选择城市');
        }
    })

    // 渲染tr
    function renderDiffTr() {
        var type = $("input[name='type']:checked").val();

        var tr = '';
        if (diff_freight_data.length > 0) {
            diff_freight_data.forEach(function (val, index) {
                var region = val.is_all ? '全国' : (val.province_text + (val.city_text && val.province_text ? ',' : '') + val.city_text);
                if (type == 1) {
                    tr += '<tr>\n' +
                      '                                <td class="count_region">' + region + '</td>\n' +
                      '                                <td>\n' +
                      '                                    <input type="number" class="form-control diff_init_count" min="1" onblur="changeDiffInitCount(this)" value="' + val.diff_init_count + '">\n' +
                      '                                </td>\n' +
                      '                                <td>\n' +
                      '                                    <input type="number" class="form-control diff_init_count_price" min="0" onblur="changeDiffInitCountPrice(this)" value="' + val.diff_init_count_price + '">\n' +
                      '                                </td>\n' +
                      '                                <td>\n' +
                      '                                    <input type="number" class="form-control diff_inc_count" min="1" onblur="changeDiffIncCount(this)" value="' + val.diff_inc_count + '">\n' +
                      '                                </td>\n' +
                      '                                <td>\n' +
                      '                                    <input type="number" class="form-control diff_inc_count_price" min="0" onblur="changeDiffIncCountPrice(this)" value="' + val.diff_inc_count_price + '">\n' +
                      '                                </td>\n' +
                      '                                <td class="project-actions">\n' +
                      '                                    <a onclick="setRegion(' + index + ')" class="btn btn-white btn-sm"><i class="iconfont icon-edit"></i> 设置城市 </a>\n' +
                      '                                    <a onclick="delRegion(' + index + ')" class="btn btn-white btn-sm"><i class="iconfont icon-trash"></i> 删除 </a>\n' +
                      '                                </td>\n' +
                      '                            </tr>';
                } else {
                    tr += '<tr>\n' +
                      '                                <td class="weight_region">' + region + '</td>\n' +
                      '                                <td>\n' +
                      '                                    <input type="number" class="form-control diff_init_weight" min="0" onblur="changeDiffInitWeight(this)" value="' + val.diff_init_weight + '">\n' +
                      '                                </td>\n' +
                      '                                <td>\n' +
                      '                                    <input type="number" class="form-control diff_init_weight_price" min="0" onblur="changeDiffInitWeightPrice(this)" value="' + val.diff_init_weight_price + '">\n' +
                      '                                </td>\n' +
                      '                                <td>\n' +
                      '                                    <input type="number" class="form-control diff_inc_weight" min="0" onblur="changeDiffIncWeigth(this)" value="' + val.diff_inc_weight + '">\n' +
                      '                                </td>\n' +
                      '                                <td>\n' +
                      '                                    <input type="number" class="form-control diff_inc_weight_price" min="0" onblur="changeDiffIncWeightPrice(this)" value="' + val.diff_inc_weight_price + '">\n' +
                      '                                </td>\n' +
                      '                                <td class="project-actions">\n' +
                      '                                    <a onclick="setRegion(' + index + ')" class="btn btn-white btn-sm"><i class="iconfont icon-edit"></i> 设置城市 </a>\n' +
                      '                                    <a onclick="delRegion(' + index + ')" class="btn btn-white btn-sm"><i class="iconfont icon-trash"></i> 删除 </a>\n' +
                      '                                </td>\n' +
                      '                            </tr>';
                }
            })
        }

        if (type == 1) {
            $('.count_tbody').html(tr);

            $('.count_table tbody').show();
            $('.weight_table tbody').hide();
        } else {
            $('.weight_tbody').html(tr);

            $('.weight_table tbody').show();
            $('.count_table tbody').hide();
        }
    }

    function changeDiffInitCount(that){
        var index = $('.diff_init_count').index(that);
        diff_freight_data[index].diff_init_count = $(that).val();

        $("input[name='diff_freight']").val(JSON.stringify(diff_freight_data));
    }

    function changeDiffInitCountPrice(that){
        var index = $('.diff_init_count_price').index(that);
        diff_freight_data[index].diff_init_count_price = $(that).val();

        $("input[name='diff_freight']").val(JSON.stringify(diff_freight_data));
    }

    function changeDiffIncCount(that) {
        var index = $('.diff_inc_count').index(that);
        diff_freight_data[index].diff_inc_count = $(that).val();

        $("input[name='diff_freight']").val(JSON.stringify(diff_freight_data));
    }

    function changeDiffIncCountPrice(that) {
        var index = $('.diff_inc_count_price').index(that);
        diff_freight_data[index].diff_inc_count_price = $(that).val();

        $("input[name='diff_freight']").val(JSON.stringify(diff_freight_data));
    }

    function changeDiffInitWeight(that) {
        var index = $('.diff_init_weight').index(that);
        diff_freight_data[index].diff_init_weight = $(that).val();

        $("input[name='diff_freight']").val(JSON.stringify(diff_freight_data));
    }

    function changeDiffInitWeightPrice(that) {
        var index = $('.diff_init_weight_price').index(that);
        diff_freight_data[index].diff_init_weight_price = $(that).val();

        $("input[name='diff_freight']").val(JSON.stringify(diff_freight_data));
    }

    function changeDiffIncWeigth(that) {
        var index = $('.diff_inc_weight').index(that);
        diff_freight_data[index].diff_inc_weight = $(that).val();

        $("input[name='diff_freight']").val(JSON.stringify(diff_freight_data));
    }

    function changeDiffIncWeightPrice(that) {
        var index = $('.diff_inc_weight_price').index(that);
        diff_freight_data[index].diff_inc_weight_price = $(that).val();

        $("input[name='diff_freight']").val(JSON.stringify(diff_freight_data));
    }
</script>
{/block}
